1137B - Camp Schedule - CodeForces Solution


greedy hashing strings *1600

Please click on ads to support us..

C++ Code:

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("bmi,bmi2,lzcnt,popcnt")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
//#pragma expected_value
//#pragma isolated_call
//#pragma disjoint

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

//using namespace __gnu_pbds;
using namespace std;

//#define int long long
//#define double long double
#define Fi first
#define Se second
#define Rep(i,a,b) for (int i=a;i<=b;++i)
#define Repu(i,b,a) for (int i=b;i>=a;--i)
#define pb push_back
#define ms(a,i) memset(a,i,sizeof(a))
#define sz size()
#define mp make_pair
#define endl '\n'
#define sef setprecision(12)<<fixed
#define cer cout<<"cak"<<endl;

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<double> va;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<va> vva;

//const double EPS=1e-9;
const double PI=acos(-1);
//const int oo=1e18;
const int MN=5e5+5;
const int mod=1e9+7;

using cd=complex<double>;

//typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> index_set;

string s,t;
int longest;

vector<int> prefix_function(const string &a){
    vector<int> pf(a.size());
    pf[0] = 0;
    for (int i=1; i<a.size(); i++){
        int ptr = pf[i-1];
        while (ptr != 0 && a[ptr] != a[i]) ptr = pf[ptr-1];
        pf[i] = ptr + (a[ptr] == a[i]);
    }
    return pf;
}

int cnt[2];

int sech[2];

int caknqu[2];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>s>>t;
    vi pf=prefix_function(t);
    longest=pf[t.sz-1];

    s=" "+s; t=" "+t;
    Rep(i,1,s.sz-1) {
        cnt[s[i]-'0']++;
    }

    Rep(i,longest+1,t.sz-1) {
        sech[t[i]-'0']++;
    }

    Rep(i,1,longest) {
        caknqu[t[i]-'0']++;
    }

    caknqu[0]+=sech[0]; caknqu[1]+=sech[1];

    if(cnt[0]<caknqu[0] or cnt[1]<caknqu[1]) {
        Rep(i,1,s.sz-1) cout<<s[i];
        return 0;
    }

    Rep(i,1,t.sz-1) cout<<t[i];
    cnt[0]-=caknqu[0]; cnt[1]-=caknqu[1];
    while(cnt[0]>=sech[0] and cnt[1]>=sech[1]) {
        Rep(i,longest+1,t.sz-1) {
            cout<<t[i];
        }
        cnt[0]-=sech[0]; cnt[1]-=sech[1];
    }

    Rep(i,1,cnt[0]) cout<<0;
    Rep(i,1,cnt[1]) cout<<1;
}


Comments

Submit
0 Comments
More Questions

1738C - Even Number Addicts
1064B - Equations of Mathematical Magic
384A - Coder
1738B - Prefix Sum Addicts
1352D - Alice Bob and Candies
1316D - Nash Matrix
1548B - Integers Have Friends
348A - Mafia
315B - Sereja and Array
204A - Little Elephant and Interval
385B - Bear and Strings
114C - Grammar Lessons
1427A - Avoiding Zero
583A - Asphalting Roads
1358B - Maria Breaks the Self-isolation
828A - Restaurant Tables
1735A - Working Week
1735D - Meta-set
1735B - Tea with Tangerines
1735C - Phase Shift
1321C - Remove Adjacent
281B - Nearest Fraction
1043A - Elections
1598C - Delete Two Elements
1400C - Binary String Reconstruction
1734D - Slime Escape
1499A - Domino on Windowsill
991A - If at first you don't succeed
1196C - Robot Breakout
373A - Collecting Beats is Fun